﻿using SF.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace SF.DAL
{
    /// <summary>
    /// 用户统计DAL
    /// </summary>
    public partial class bs_user_total
    {
        public bs_user_total()
        {

        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long Add(SF.Model.bs_user_total model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into bs_user_total(");
            strSql.Append("userId, totalMoney, totalLevelAward, totalShoppingCurrency, totalActionCurrency, totalPublicFund, totalExp)");
            strSql.Append(" values(");
            strSql.Append("@userId, @totalMoney, @totalLevelAward, @totalShoppingCurrency, @totalActionCurrency, @totalPublicFund, @totalExp)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.BigInt),
					new SqlParameter("@totalMoney", SqlDbType.Decimal,5),
					new SqlParameter("@totalLevelAward", SqlDbType.Decimal,5),
					new SqlParameter("@totalShoppingCurrency", SqlDbType.Decimal,5),
					new SqlParameter("@totalActionCurrency", SqlDbType.Decimal,5),
					new SqlParameter("@totalPublicFund", SqlDbType.Decimal,5),
					new SqlParameter("@totalExp", SqlDbType.Decimal,5)};
            parameters[0].Value = model.userId;
            parameters[1].Value = model.totalMoney;
            parameters[2].Value = model.totalLevelAward;
            parameters[3].Value = model.totalShoppingCurrency;
            parameters[4].Value = model.totalActionCurrency;
            parameters[5].Value = model.totalPublicFund;
            parameters[6].Value = model.totalExp;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SF.Model.bs_user_total model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update bs_user_total set ");
            strSql.Append("totalMoney=@totalMoney,");
            strSql.Append("totalLevelAward=@totalLevelAward,");
            strSql.Append("totalShoppingCurrency=@totalShoppingCurrency,");
            strSql.Append("totalActionCurrency=@totalActionCurrency,");
            strSql.Append("totalPublicFund=@totalPublicFund,");
            strSql.Append("totalExp=@totalExp");
            strSql.Append(" where userId=@userId");
            SqlParameter[] parameters = {
					new SqlParameter("@totalMoney", SqlDbType.Decimal,5),
					new SqlParameter("@totalLevelAward", SqlDbType.Decimal,5),
					new SqlParameter("@totalShoppingCurrency", SqlDbType.Decimal,5),
					new SqlParameter("@totalActionCurrency", SqlDbType.Decimal,5),
					new SqlParameter("@totalPublicFund", SqlDbType.Decimal,5),
					new SqlParameter("@totalExp", SqlDbType.Decimal,5),
					new SqlParameter("@userId", SqlDbType.BigInt)};
            parameters[0].Value = model.totalMoney;
            parameters[1].Value = model.totalLevelAward;
            parameters[2].Value = model.totalShoppingCurrency;
            parameters[3].Value = model.totalActionCurrency;
            parameters[4].Value = model.totalPublicFund;
            parameters[5].Value = model.totalExp;
            parameters[6].Value = model.userId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long userId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from bs_user_total ");
            strSql.Append(" where userId=@userId");
            SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.BigInt)
			};
            parameters[0].Value = userId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="busnieseNum"></param>
        /// <param name="appNum"></param>
        /// <returns></returns>
        public SF.Model.bs_user_total GetModel(long userId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id, userId, totalMoney, totalLevelAward, totalShoppingCurrency, totalActionCurrency, totalPublicFund, totalExp from bs_user_total ");
            strSql.Append(" where userId=@userId");
            SqlParameter[] parameters = {
					new SqlParameter("@userId", SqlDbType.BigInt),
			};
            parameters[0].Value = userId;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.bs_user_total DataRowToModel(DataRow row)
        {
            SF.Model.bs_user_total model = new SF.Model.bs_user_total();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = long.Parse(row["id"].ToString());
                }
                if (row["userId"] != null && row["userId"].ToString() != "")
                {
                    model.userId = long.Parse(row["userId"].ToString());
                }
                if (row["totalMoney"] != null && row["totalMoney"].ToString() != "")
                {
                    model.totalMoney = decimal.Parse(row["totalMoney"].ToString());
                }
                if (row["totalLevelAward"] != null && row["totalLevelAward"].ToString() != "")
                {
                    model.totalLevelAward = decimal.Parse(row["totalLevelAward"].ToString());
                }
                if (row["totalShoppingCurrency"] != null && row["totalShoppingCurrency"].ToString() != "")
                {
                    model.totalShoppingCurrency = decimal.Parse(row["totalShoppingCurrency"].ToString());
                }
                if (row["totalActionCurrency"] != null && row["totalActionCurrency"].ToString() != "")
                {
                    model.totalActionCurrency = int.Parse(row["totalActionCurrency"].ToString());
                }
                if (row["totalPublicFund"] != null && row["totalPublicFund"].ToString() != "")
                {
                    model.totalPublicFund = decimal.Parse(row["totalPublicFund"].ToString());
                }
                if (row["totalExp"] != null && row["totalExp"].ToString() != "")
                {
                    model.totalExp = decimal.Parse(row["totalExp"].ToString());
                }
            }
            return model;
        }
    }
}
